package br.com.galante.webapp.view;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

@Named("usuario")
@RequestScoped
public class UsuarioView {

	public String logout(){
		String result = "/home.jsf";
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.
                getExternalContext().getRequest();
        try {
            System.out.println("calling logout");
            request.logout();
            System.out.println("called logout");
        } catch (ServletException ex) {
            Logger.getLogger(UsuarioView.class.getName()).
                    log(Level.SEVERE, null, ex);
            result = "/public/erro.jsf";
        }
        return result;
	}
	public boolean isSigned() {
		return (getUserName() != null);
	}
	
	public String getUserName(){
		FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.
                getExternalContext().getRequest();
		return request.getRemoteUser();
	}
}
